<template>
  <div>
    <!-- 背景 -->
    <div class="Bg">
      <!-- 背景顶部 -->
      <div class="BgTop">
        <img src="/BG.png" alt="" class="bd" />
      </div>
      <div class="BgContent">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-zuojiantou"></use>
        </svg>
        <div class="BgText">
          <div>登录页</div>
          <span>Login User Interface</span>
        </div>
      </div>
    </div>
    <!-- 输入框表单 -->
    <div class="LoginInput">
      <van-form>
        <div class="d-icon-1">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-aite"></use>
          </svg>
        </div>
        <div class="d-icon-2">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-yuechi"></use>
          </svg>
        </div>
        <van-field
          v-model="username"
          name="用户名"
          placeholder="请输入用户名"
          :rules="[{ validator: isName, message: '请输入正确内容' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          placeholder="请输入密码"
          :rules="[{ validator: isPwd, message: '请填写正确内容' }]"
        />
        <!-- 去注册 -->
        <div class="reg">
          <span><a href="/register">注册账号</a></span>
          <span><a href="">忘记密码？</a></span>
        </div>
        <!-- 提交按钮 -->
        <div class="submit" @click.capture="checkForm">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-youjiantou"></use>
          </svg>
        </div>
        <!-- qq wx -->
        <div class="qq_wx">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-QQ"></use>
          </svg>
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-weixin"></use>
          </svg>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    return {
      username: "",
      password: "",
      activeName: "a", //验证码或者密码
    };
  },
  methods: {
    // 校验函数返回 true 表示校验通过，false 表示不通过
    isName() {
      // console.log(this.username);
      return /^1[3-9]\d{9}$/.test(this.username);
    },
    isPwd() {
      // console.log(this.password);
      //以字母开头，长度在6~18之间，只能包含字母、数字和下划线
      return /^[a-zA-Z]\w{5,17}$/.test(this.password);
    },

    //表单验证后发请求登录
    checkForm() {
      // console.log(this.isName());
      // console.log(this.isPwd());
      if (this.isName() && this.isPwd()) {
        //获取参数
        var params = `username=${this.username}&password=${this.password}`;
        this.axios.post("/login", params).then(res => {
          console.log(res);
          if (res.data.code == 200) {
            console.log("登录通过，即将跳转");
            this.$router.push("/my");
          } else {
            console.log("登陆失败，用户名不存在");
            alert("登陆失败，用户名不存在");
          }
        });
      }
    },
  },
};
</script>

<style lang="scss">
.bd {
  display: inline-block;
  width: 38rem;
  position: fixed;
  top: -11.6rem;
  left: -6.6rem;
  z-index: -10;
}
.BgContent {
  .icon {
    fill: #fff;
    width: 1.4rem;
    height: 1.4rem;
    margin: 2.5rem 0.4rem 2rem;
  }
  .BgText {
    color: #fff;
    margin: 0.1rem 4rem;
    > div {
      font-size: 1.8rem;
    }
    > span {
      font-size: 0.4rem;
      display: inline-block;
      margin-top: 1rem;
    }
  }
}
.LoginInput {
  display: flex;
  padding: 3.5rem 2rem;
  .van-form {
    margin: 0 auto;
  }
  .van-cell {
    margin: 1rem 0rem;
    padding: 0.7rem 0.5rem;
    width: 19.5rem;
    height: 2.7rem;
    border-radius: 5px 5px, 0px, 0px;
    background: rgb(255, 255, 255);
    box-shadow: 1px 1px 6px 0px rgb(5 5 10);
  }
  .van-field__error-message {
    position: absolute;
    top: 0rem;
    right: 0.4rem;
  }
  .van-field__control {
    width: 90%;
    margin-left: 3rem;
  }
  .d-icon-1 {
    position: absolute;
    top: 15.3rem;
    z-index: 5;
  }
  .d-icon-2 {
    position: absolute;
    top: 19rem;
    z-index: 5;
  }
  .icon {
    width: 1.5rem;
    height: 1.5rem;
    padding: 0.5rem;
    fill: #868689;
  }
  .reg {
    display: flex;

    > span > a {
      display: flex;
      font-size: 0.9rem;
      margin-left: 0.5rem;
      margin-right: 1.2rem;
      color: #868689;
      align-content: space-between;
    }
  }
  .submit {
    background-color: #fa5a5a;
    width: 3rem;
    height: 3rem;
    border-radius: 50%;
    margin-left: 15rem;
    margin-top: 3rem;
    .icon {
      width: 2rem;
      height: 2rem;
      padding: 0.5rem;
      fill: #fff;
    }
  }
  .qq_wx {
    margin-top: 1.5rem;
    width: 100%;
    display: flex;
    justify-content: space-around;
    > .icon {
      width: 2.6rem;
      height: 2.6rem;
      display: flex;
    }
  }
}
</style>
